const app = getApp();
var upload = require('../../../utils/upload');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    islogin:false,
    isreg:true,
    uid:0,
    startBarHeight:0,
    navgationHeight:0,
    checked:false,
    logo:'',
    skin:'',
    phone_uid:'',
    title:'城市号名称',
    note:'城市号简介',
    mp:{fans: 0,like:0,views:0}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      startBarHeight: app.globalData.startBarHeight,
      navgationHeight: app.globalData.navgationHeight,
    });
    app.loginState(() => {
      this.infoMp();
    },false)
  },
  /**
   * 是否登录判断
   */
  onShow:function () {
    app.loginState((rel) => {
      app.api().Get('openapi/v1/user/getUserInfo',(res) => {
        if (res.code == 200) {
          this.setData({
            phone_uid: res.data.phone_uid,
            uid:app.globalData.loginuser.uid
          })
          if(this.data.isreg){
            this.infoMp();
          }
        }
      })
    },true);
  },
  //获取城市号信息
  infoMp: function () {
    var param = 
    app.api().Get('api/v1/ais/infosend/getMp',{uid:this.data.uid},(result)=>{
      if(result.code == 200){
        this.setData({
          mp: result.data,
          title: result.data.title,
          note: result.data.note,
          logo:result.data.logo,
          skin:result.data.skin,
          checked:result.data.is_vip || result.data.is_apply ? true : false,
          isreg:false
        })
      }
    })
  },
  //提交数据
  formSubmit:function(e){
    var data = e.detail.value;
    if (app.util().isNull(data.title)) {
      app.wxAlert('请输入城市号名称');
      return;
    }
    if (app.util().isNull(data.note)){
      app.wxAlert('请输入城市号简介');
      return;
    }
    if (app.util().isNull(data.telphone)) {
      app.wxAlert('请输入手机号');
      return;
    }
    if(this.data.checked){
      if (app.util().isNull(data.vip_title) || app.util().isNull(data.vip_about)) {
        app.wxAlert('认证信息必须填写');
        return;
      }
    }
    wx.showLoading({
      title: '信息发布中',
      mask: true
    })
    var data = e.detail.value;
    upload.Upfiles('api/v1/ais/infosend/upimg',this.data.tempLogo,(file) => {
    if (file.isend == true){
        var logo = app.util().isNull(file.filePaths) ? '': file.filePaths[0];
        upload.Upfiles('api/v1/ais/infosend/upimg',this.data.tempSkin,(file) => {
          if (file.isend == true){
            var skin = app.util().isNull(file.filePaths) ? '':file.filePaths[0];
            var parms = {
              logo: logo,
              skin: skin,
              title: data.title,
              note: data.note,
              telphone: data.telphone,
              vip_title: data.vip_title,
              vip_about: data.vip_about,
              checked:this.data.checked?1:0,
            }
            app.api().Post('api/v1/ais/infosend/editMp',parms,(rel) => {
              wx.hideLoading()
              app.wxAlert(rel.msg,()=>{
                wx.navigateBack({
                  delta:1,
                  fail:function(){
                    wx.switchTab({
                      url: 'index',
                    })
                  }
                })
              })
            })
          }
        })
      }
    })
  },
  //名称
  onTitle: function (e) {
    this.setData({
      title:app.util().isNull(e.detail)?'城市号名称':e.detail
    })
  },
  //简介
  onNote: function (e) {
    this.setData({
      note:app.util().isNull(e.detail)?'城市号简介':e.detail
    })
  },
  //计划电话
  onPhone: function (e) {
    wx.navigateTo({
      url: '/packageHelper/pages/bindphone',
    })
  },
  /*上传头像 */
  upFace: function () {
    let that = this;
    wx.chooseImage({ 
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        that.data.tempLogo = res.tempFilePaths
        that.setData({
          logo:that.data.tempLogo[0]
        })
      }
    })
  },
  /*上传头像 */
  upSkin: function () {
    let that = this;
    wx.chooseImage({ 
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        that.data.tempSkin = res.tempFilePaths
        that.setData({
          skin:that.data.tempSkin[0]
        })
      }
    })
  },
  //提交认证信息
  onChange: function (e) {
    this.setData({
      checked: !this.data.checked
    })
  },
  //返回上一页
  onClickLeft:function(){
    wx.navigateBack({
      delta: 0,
    })
  }
})